import { WindowsPlatform } from './WindowsPlatform';
import { LinuxPlatform } from './LinuxPlatform';
import { AbstractPlatform } from './AbstractPlatform';
import { MacOSPlatform } from './MacOSPlatform';
import { NetworkAdapter } from '../core/NetworkAdapter';
import { HTTPAdapter, TCPAdapter, MQTTAdapter } from '../core/NetworkAdapter';

export class PlatformFactory {
    private static currentPlatform: AbstractPlatform;

    static getPlatform(): AbstractPlatform {
        if (this.currentPlatform) {
            return this.currentPlatform;
        }

        const platform = process.platform;
        switch (platform) {
            case 'win32':
                this.currentPlatform = new WindowsPlatform();
                break;
            case 'linux':
                this.currentPlatform = new LinuxPlatform();
                break;
            case 'darwin':
                this.currentPlatform = new MacOSPlatform();
                break;
            default:
                throw new Error(`Unsupported platform: ${platform}`);
        }

        return this.currentPlatform;
    }

    private static adapters = new Map<string, NetworkAdapter>();

    static setPlatform(platform: AbstractPlatform) {
        this.currentPlatform = platform;
    }

    static getAdapter(protocol: string): NetworkAdapter {
        if (this.adapters.has(protocol)) {
            return this.adapters.get(protocol)!;
        }

        let adapter: NetworkAdapter;
        switch (protocol.toLowerCase()) {
            case 'http':
                adapter = new HTTPAdapter();
                break;
            case 'tcp':
                adapter = new TCPAdapter();
                break;
            case 'mqtt':
                adapter = new MQTTAdapter();
                break;
            default:
                throw new Error(`Unsupported protocol: ${protocol}`);
        }

        this.adapters.set(protocol, adapter);
        return adapter;
    }

    static detectAvailableDrivers(): string[] {
        const drivers: string[] = [];
        const platform = this.getPlatform();
        
        // 检测系统驱动
        if (platform.hasDriver('network')) {
            drivers.push('network');
        }
        
        // 检测其他驱动...
        return drivers;
    }
}